package com.qxl.forest.business.service.Implement;

import com.qxl.forest.business.service.MenuService;
import com.qxl.forest.business.persistence.dao.MenuDao;
import com.qxl.forest.business.persistence.dao.RelationDao;
import com.qxl.forest.business.persistence.entity.Menu;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @Author: qiuxinlin
 * @Dercription:
 * @Date: 17:24 2017/11/24
 */
@Service
public class MenuServiceImpl implements MenuService {
    @Autowired
    MenuDao menuDao;
    @Autowired
    RelationDao relationDao;

    @Override
    public void delMenuContainSubMenus(Integer menuId) {
        Menu menu = menuDao.findOne(menuId);
        //删除当前菜单
        delMenu(menuId);
        //删除所有子菜单
        List<Menu> menus = menuDao.findByPcodesLike("[" + menu.getCode() + "]");
        for (Menu temp : menus) {
            delMenu(temp.getId());
        }
    }

    @Override
    @Transactional
    public void delMenu(Integer menuId) {
        //删除菜单
        menuDao.delete(menuId);
        //删除关联的relation
        relationDao.deleteByMenuid(menuId);
    }
}
